<template>
  <el-card>
    <el-tabs v-model="activeTab" type="border-card">
      <!-- 成交基础信息 -->
      <el-tab-pane label="成交基础信息" name="base">
        <DealBaseInfo
          @update-dealBaseInfo="updateDeal"
          v-if="dataLoad"
          ref="dealBaseInfo"
          :msgData="dealBase"
        />
      </el-tab-pane>

      <!-- 成交分成信息 -->
      <el-tab-pane label="成交分成信息" name="divide">
        <DealDivideSection
          v-if="dataLoad"
          :msgData="dealBase"
          v-model:data="dealDivideData"
        />
      </el-tab-pane>

      <!-- 房源信息服务 -->
      <el-tab-pane label="房源信息服务" name="houseInfomation">
        <DealHouseInfomation
          v-if="dataLoad"
          ref="dealHose"
          :fData="dealBase"
          :msgData="dealHoseInfomation"
        />
      </el-tab-pane>
      <!-- 交易服务费分成情况 -->
      <el-tab-pane label="交易服务费分成情况" name="transaction">
        <DealTransaction
          v-if="dataLoad"
          v-model:data="dealTransaction"
          :fData="dealBase"
        />
      </el-tab-pane>
      <!-- 居间服务分成 -->
      <el-tab-pane label="居间服务分成信息" name="cooperation">
        <DealCooperationSection
          v-if="dataLoad"
          :msgData="dealBase"
          v-model:data="dealCooperationData"
        />
      </el-tab-pane>
    </el-tabs>

    <!-- 操作按钮 -->
    <!-- <div class="actions" v-if="activeTab == 'cooperation'">
      <el-button :loading="subLoad" type="primary" @click="submit"
        >保存</el-button
      >
    </div> -->
  </el-card>
</template>

<script setup>
import { ref, onMounted } from "vue";
import DealBaseInfo from "./components/DealBaseInfo.vue";
import DealDivideSection from "./components/DealDivideSection.vue";
import DealCooperationSection from "./components/DealCooperationSection.vue";
import DealHouseInfomation from "./components/DealHouseInfomation.vue";
import DealTransaction from "./components/DealTransaction.vue";
import { transactionConfirm, editTcForm } from "@/api/business/tcForm";
const route = useRoute();
// 当前激活 tab
const activeTab = ref("base");
// 各子组件数据（v-model）
const dealBaseInfo = ref();
const dealDivideData = ref([]);
const dealHoseInfomation = ref({});
const dealCooperationData = ref([]);
const subLoad = ref(false);
const dealTransaction = ref({
  branchManagerName: "",
  storeAmount: "",
  companyAmount: "",
});
const dealBase = ref({});
const dataLoad = ref(false);
const { proxy } = getCurrentInstance();
onMounted(async () => {
  await getDealInfo();
});
async function getDealInfo() {
  const res = await transactionConfirm(route.query.id);
  console.log("gengxin");
  const {
    sysHzCommissionDistributionList,
    sysHzRoomServiceFeeDistributionList,
    sysHzTransactionServiceFeeDistribution,
    sysHzHouseInformationList,
    ...reset
  } = res.data;
  dealBase.value = { ...reset };

  dealDivideData.value =
    sysHzCommissionDistributionList == null
      ? []
      : sysHzCommissionDistributionList;

  dealHoseInfomation.value =
    sysHzHouseInformationList == null ? {} : sysHzHouseInformationList;
  if (sysHzTransactionServiceFeeDistribution) {
    dealTransaction.value = sysHzTransactionServiceFeeDistribution;
  }

  dealCooperationData.value =
    sysHzRoomServiceFeeDistributionList == null
      ? []
      : sysHzRoomServiceFeeDistributionList;

  dataLoad.value = true;
}
async function updateDeal(payload) {
  console.log(payload);
  if (payload) {
    await getDealInfo();
  }
}

const dealHose = ref();

const submit = () => {
  const payload = {
    ...dealBaseInfo.value.form,
    sysHzCommissionDistributionList: dealDivideData.value,
    sysHzHouseInformationList: dealHose.value.form1,
    sysHzTransactionServiceFeeDistribution: dealTransaction.value,
    sysHzRoomServiceFeeDistributionList: dealCooperationData.value,
  };
  //验证成交分成信息分成比例之和为100
  const allCount = payload.sysHzCommissionDistributionList.reduce(
    (n, v) => n + v.distributionRatio,
    0
  );

  if (allCount != 100) {
    proxy.$modal.msgError("成交分成信息中所有分成比例相加之和应该为100");
    return;
  }
  //验证居间服务分成信息分成比例之和为100
  const cooperationCount = payload.sysHzRoomServiceFeeDistributionList.reduce(
    (n, v) => n + v.distributionRatio,
    0
  );
  if (cooperationCount != 100) {
    proxy.$modal.msgError("居间服务分成信息中所有分成比例相加之和应该为100");
    return;
  }
  subLoad.value = true;
  // TODO: 调用接口提交
  editTcForm(payload)
    .then((res) => {
      subLoad.value = false;
      proxy.$modal.msgSuccess("修改成功");
    })
    .catch((err) => {
      subLoad.value = false;
    });
};
</script>

<style scoped>
.actions {
  text-align: right;
  margin-top: 20px;
}
</style>
